import parole
from parole.colornames import colors
from parole.display import interpolateRGB
import pygame, random

import sim, main, util, sim_items
from util import *

class Fungus(sim_items.LightSource):
    def __init__(self):
        sim_items.LightSource.__init__(self,
                'mycena lucentipes stalk',     #name
                50,     # layer
                parole.map.AsciiTile('*', colors['Chartreuse']),
                .5,  # weight
                2500,   # light
                colors[random.choice(['Chartreuse', 'Lime', 'DarkTurquoise', 'DeepSkyBlue'])],  # light color
                0.5,    # light intensity
                12000*60000,  # burn time
                unidDescription="A large mushroom capable of emitting a soft greenish "\
                    "light by virture of its innate bioluminescence.",
                showBurnTime=False,
                dousable=False,
                litAdjective="glowing",
                handsNeeded=0)

        self.snuffable = False
        self.startIntensity = self.lightIntensity
        self.startBurnTime = self.burnTime
        main.schedule.add(sim.Event('activate light', main.schedule.time,
            (self,), dests=[self]))
        self.fading = False
        self.gutterVerb = 'flickers'

    def listen(self, event):
        super(Fungus, self).listen(event)
        if event.id == 'enter inventory':
            item, creature = event.args
            if item is self:
                # someone picked us up. start fading out like a flare
                self.fading = True
                self.burnTime = 2*util.MINUTE
                self.startBurnTime = self.burnTime
                if visible(self) or (self.carrier and\
                        visible(self.carrier)):
                    pos = self.carrier and self.carrier.pos or self.pos
                    main.transient('Once picked, the bioluminescence of %s '\
                            'begins to fade rapidly.' % self.noun(definite=1),
                            pos)
        elif event.id == 'burn' and event.args[0] is self:
            if not self.burnTime or not self.fading:
                return 
            newIntensity = (float(self.burnTime) / self.startBurnTime) * \
                            self.startIntensity
            main.schedule.add(sim.Event('update light', main.schedule.time,
                (self, self.lightRGB, newIntensity), dests=[self]))

#========================================
thingClass = Fungus
